﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;
    using WMS6.Web.UserControls;

    public class LoadingList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F7057;
        protected GridView gvLoadHeader;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLoadID;
        protected HtmlHead Head1;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblLoadHeader;
        protected Label lblMsg;
        protected LinkButton lnkbtnClose;
        protected LinkButton lnkbtnServed;
        protected LinkButton lnkbtnShipping;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnClose;
        protected HtmlTableCell tdlnkbtnServed;
        protected HtmlTableCell tdlnkbtnShipping;
        protected UpdatePanel up1;

        private void BindData2LoadList(LoadHeaderQueryEntity loadHeaderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(loadHeaderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundService2ClientProxy.GetInstance().GetLoadByQueryEntity(loadHeaderQuery);
            base.BindData2GridView(this.gvLoadHeader, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLoadHeader, "LOAD_ID");
            List<string> status = base.GetSelectedIDsInGridView(this.gvLoadHeader, "STATUS");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请至少选择一项进行删除操作";
            }
            else
            {
                for (int i = 0; status.Count > i; i++)
                {
                    if (status[i].ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "装车单号" + selectedID[i] + "状态不等于新建无法删除!";
                        return;
                    }
                }
                try
                {
                    this.lblMsg.Text = "删除装车单号:" + OutboundService2ClientProxy.GetInstance().DeleteLoadInfo(selectedID) + "成功!";
                    LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
                    this.BindData2LoadList(loadHeaderQuery);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLoadHeader, "LOAD_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项进行编辑";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项进行编辑";
            }
            else
            {
                base.Response.Redirect(string.Format("Loading.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            loadHeaderQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = loadHeaderQuery.CurrentPage;
            loadHeaderQuery.CurrentPage = "0";
            this.gvLoadHeader.Columns[1].Visible = false;
            this.gvLoadHeader.AllowSorting = false;
            this.BindData2LoadList(loadHeaderQuery);
            this.hdnActionID.Value = "";
            this.gvLoadHeader.ShowFooter = false;
            loadHeaderQuery.IsGetAll = false;
            loadHeaderQuery.CurrentPage = currentPageIndex;
            this.SetLoadQueryEntity(loadHeaderQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Loading.xls", this.gvLoadHeader);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            loadHeaderQuery.CurrentPage = "0";
            this.BindData2LoadList(loadHeaderQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            loadHeaderQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2LoadList(loadHeaderQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            loadHeaderQuery.LoadID = "";
            loadHeaderQuery.ExternalID = "";
            loadHeaderQuery.Status = "";
            loadHeaderQuery.DepartureTime = "";
            loadHeaderQuery.CarrierID = "";
            loadHeaderQuery.Route = "";
            loadHeaderQuery.Door = "";
            loadHeaderQuery.VehicleNumber = "";
            loadHeaderQuery.CreatedBy = "";
            loadHeaderQuery.CreatedDate = "";
            this.BindData2LoadList(loadHeaderQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            if (int.Parse(loadHeaderQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                loadHeaderQuery.CurrentPage = (int.Parse(loadHeaderQuery.CurrentPage) + 1).ToString();
                this.BindData2LoadList(loadHeaderQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
            if (int.Parse(loadHeaderQuery.CurrentPage) > 0)
            {
                loadHeaderQuery.CurrentPage = (int.Parse(loadHeaderQuery.CurrentPage) - 1).ToString();
                this.BindData2LoadList(loadHeaderQuery);
            }
        }

        private LoadHeaderQueryEntity GetLoadQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as LoadHeaderQueryEntity);
            }
            return (base.PageQueryEntitySession as LoadHeaderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
                loadHeaderQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2LoadList(loadHeaderQuery);
            }
        }

        protected void gvLoadHeader_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7057", this.gvLoadHeader);
        }

        protected void gvLoadHeader_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvLoadHeader_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LoadHeaderQueryEntity loadHeaderQuery = this.GetLoadQueryEntity();
                loadHeaderQuery.SortField = e.SortExpression;
                if (loadHeaderQuery.SortDirection == "ASC")
                {
                    loadHeaderQuery.SortDirection = "DESC";
                }
                else if (loadHeaderQuery.SortDirection == "DESC")
                {
                    loadHeaderQuery.SortDirection = "ASC";
                }
                this.BindData2LoadList(loadHeaderQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnClose_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnServed_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnShipping_Click(object sender, EventArgs e)
        {
        }

        private void LoadData()
        {
            LoadHeaderQueryEntity loadHeaderQuery = new LoadHeaderQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    loadHeaderQuery.LoadID = this.PreviousPage.LoadID;
                    loadHeaderQuery.ExternalID = this.PreviousPage.ExternalID;
                    loadHeaderQuery.Status = this.PreviousPage.Status;
                    loadHeaderQuery.DepartureTime = this.PreviousPage.DepartureTime;
                    loadHeaderQuery.CarrierID = this.PreviousPage.CarrierID;
                    loadHeaderQuery.Route = this.PreviousPage.Route;
                    loadHeaderQuery.Door = this.PreviousPage.Door;
                    loadHeaderQuery.VehicleNumber = this.PreviousPage.VehicleNumber;
                    loadHeaderQuery.CreatedBy = this.PreviousPage.CreatedBy;
                    loadHeaderQuery.CreatedDate = this.PreviousPage.CreatedDate;
                }
                else
                {
                    loadHeaderQuery.LoadID = "";
                    loadHeaderQuery.ExternalID = "";
                    loadHeaderQuery.Status = "";
                    loadHeaderQuery.DepartureTime = "";
                    loadHeaderQuery.CarrierID = "";
                    loadHeaderQuery.Route = "";
                    loadHeaderQuery.Door = "";
                    loadHeaderQuery.VehicleNumber = "";
                    loadHeaderQuery.CreatedBy = "";
                    loadHeaderQuery.CreatedDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            loadHeaderQuery.IsGetAll = false;
            loadHeaderQuery.CurrentPage = "0";
            loadHeaderQuery.PageSize = this.gvLoadHeader.PageSize.ToString();
            loadHeaderQuery.SortField = "LOAD_ID";
            loadHeaderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvLoadHeader.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetLoadQueryEntity(loadHeaderQuery);
            this.BindData2LoadList(loadHeaderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLoadQueryEntity(LoadHeaderQueryEntity loadHeaderQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = loadHeaderQuery;
            }
            else
            {
                base.PageQueryEntitySession = loadHeaderQuery;
            }
        }

        public LoadingQuery PreviousPage
        {
            get
            {
                return (LoadingQuery) base.PreviousPage;
            }
        }
    }
}

